namespace PickGold.Charting
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Globalization;

    internal class ColorArrayConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
        }

        public static string ColorArrayToString(Color[] colors)
        {
            if ((colors == null) || (colors.GetLength(0) <= 0))
            {
                return string.Empty;
            }
            ColorConverter converter = new ColorConverter();
            string str = string.Empty;
            foreach (Color color in colors)
            {
                if (str.Length > 0)
                {
                    str = str + "; ";
                }
                str = str + converter.ConvertToInvariantString(color);
            }
            return str;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string colorNames = value as string;
            if (colorNames != null)
            {
                return StringToColorArray(colorNames);
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return ColorArrayToString(value as Color[]);
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public static Color[] StringToColorArray(string colorNames)
        {
            ColorConverter converter = new ColorConverter();
            Color[] colorArray = new Color[0];
            if (colorNames.Length > 0)
            {
                string[] strArray = colorNames.Split(new char[] { ';' });
                colorArray = new Color[strArray.Length];
                int num = 0;
                foreach (string str in strArray)
                {
                    colorArray[num++] = (Color) converter.ConvertFromInvariantString(str);
                }
            }
            return colorArray;
        }
    }
}

